package cn.imvc.app.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.imvc.app.entities.Cart;
import cn.imvc.app.entities.Member;
import cn.imvc.app.models.CartModel;
import cn.imvc.app.models.MemberModel;

@Controller
@RequestMapping("/cart")
public class CartControllerImpl implements CartController {
	@Autowired
	private CartModel cartModel;
	@Autowired
	private MemberModel memberModel;

	private final int cookieMaxAge = 24 * 60 * 60;

	@RequestMapping("/list.html")
	public String list() {
/*		Member member = memberModel.getLoginMember(token);
		if (member == null) {// 未登录，显示本地cookie
			List<Cart> cartItems = cartModel.getCookieCartItems(cc);
			model.addAttribute("cartItems", cartItems);
			model.addAttribute("synchronizeCookieStr", cc);
		} else {
			model.addAllAttributes(cartModel.updateAndListCartItems(member, cc));
			// 放回前台 同步本地cookie
			// CookieTools.addCookie(response, "cartItems", null, 0);
		}*/
		return "cart/list";
	}
	@RequestMapping("/listCartItems.html")
	public String listCartItems(String cc, String token, Model model) {
		Member member = memberModel.getLoginMember(token);
		if (member == null) {// 未登录，显示本地cookie
			List<Cart> cartItems = cartModel.getCookieCartItems(cc);
			model.addAttribute("cartItems", cartItems);
			model.addAttribute("synchronizeCookieStr", cc);
		} else {
			model.addAllAttributes(cartModel.updateAndListCartItems(member, cc));
			// 放回前台 同步本地cookie
			// CookieTools.addCookie(response, "cartItems", null, 0);
		}
		return "cart/content";
	}
	//-------pc--------
	@RequestMapping("/list.htm")
	public String pcList() {
		/*		Member member = memberModel.getLoginMember(token);
		if (member == null) {// 未登录，显示本地cookie
			List<Cart> cartItems = cartModel.getCookieCartItems(cc);
			model.addAttribute("cartItems", cartItems);
			model.addAttribute("synchronizeCookieStr", cc);
		} else {
			model.addAllAttributes(cartModel.updateAndListCartItems(member, cc));
			// 放回前台 同步本地cookie
			// CookieTools.addCookie(response, "cartItems", null, 0);
		}*/
		return "cart/pclist";
	}
	@RequestMapping("/listCartItems.htm")
	public String pcListCartItems(String cc, String token, Model model) {
		Member member = memberModel.getLoginMember(token);
		if (member == null) {// 未登录，显示本地cookie
			List<Cart> cartItems = cartModel.getCookieCartItems(cc);
			model.addAttribute("cartItems", cartItems);
			model.addAttribute("synchronizeCookieStr", cc);
		} else {
			model.addAllAttributes(cartModel.updateAndListCartItems(member, cc));
			// 放回前台 同步本地cookie
			// CookieTools.addCookie(response, "cartItems", null, 0);
		}
		return "cart/pccontent";
	}
	

}
